<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Document</title>
    <script src='../vue.js'></script>
</head>
<body>
    <div id='app'>
        <button @click="addPersonBefore">在前边添加一个人</button>
        <button @click="addPersonAfter">在后边添加一个人</button>
        <ul>
            <!-- 
                在渲染的过程中 diff算法是一直在工作的,但是diff算法的比较是默认按照顺序依次比较的
                如果我们逆序添加数据 则很可能diff算法的对比错位了
                所以我们在列表渲染的时候需要添加一个唯一的key,diff算法的比较就变成了key对应的新旧虚拟DOM的比较
             -->
            <li v-for="item in persons" :key="item.id">
                姓名:{{item.name}}---年龄:{{item.age}}
            </li>
        </ul>
    </div>
    <script>
        Vue.config.productionTip = false;
        const vm = new Vue({
            el:'#app',
            data(){
                return {
                persons:[
                    {id:'001',name:'laowang',age:19},
                    {id:'002',name:'laoli',age:20},
                    {id:'003',name:'laozhang',age:21}
                ]
            }
            },
            methods:{
                addPersonBefore(){
                    this.persons=[
                        {id:Date.now(), name:'shangbian',age:18},
                        ...this.persons
                    ]
                } ,
                addPersonAfter(){
                    this.persons=[
                        ...this.persons,
                        {id:Date.now(), name:'shangbian',age:18}
                    ]   
                }
            }
    })
    </script>
</body>
</html>